<template>
  <el-dialog
    :title="!dataForm.orId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orOrderSn">
      <el-input v-model="dataForm.orOrderSn" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="发布订单的用户id" prop="orMemberId">
      <el-input v-model="dataForm.orMemberId" placeholder="发布订单的用户id"></el-input>
    </el-form-item>
    <el-form-item label="领取订单的id" prop="orRecipientId">
      <el-input v-model="dataForm.orRecipientId" placeholder="领取订单的id"></el-input>
    </el-form-item>
    <el-form-item label="订单所属分类" prop="orOrderClassifyId">
      <el-input v-model="dataForm.orOrderClassifyId" placeholder="订单所属分类"></el-input>
    </el-form-item>
    <el-form-item label="订单总价" prop="orOrderTotalPrice">
      <el-input v-model="dataForm.orOrderTotalPrice" placeholder="订单总价"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orOrderState">
      <el-input v-model="dataForm.orOrderState" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="店铺id" prop="orBuId">
      <el-input v-model="dataForm.orBuId" placeholder="店铺id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="orCreateDate">
      <el-input v-model="dataForm.orCreateDate" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orId: 0,
          orOrderSn: '',
          orMemberId: '',
          orRecipientId: '',
          orOrderClassifyId: '',
          orOrderTotalPrice: '',
          orOrderState: '',
          orBuId: '',
          orCreateDate: ''
        },
        dataRule: {
          orOrderSn: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          orMemberId: [
            { required: true, message: '发布订单的用户id不能为空', trigger: 'blur' }
          ],
          orRecipientId: [
            { required: true, message: '领取订单的id不能为空', trigger: 'blur' }
          ],
          orOrderClassifyId: [
            { required: true, message: '订单所属分类不能为空', trigger: 'blur' }
          ],
          orOrderTotalPrice: [
            { required: true, message: '订单总价不能为空', trigger: 'blur' }
          ],
          orOrderState: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          orBuId: [
            { required: true, message: '店铺id不能为空', trigger: 'blur' }
          ],
          orCreateDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orId) {
            this.$http({
              url: this.$http.adornUrl(`/sulingoxqg/order/info/${this.dataForm.orId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orOrderSn = data.order.orOrderSn
                this.dataForm.orMemberId = data.order.orMemberId
                this.dataForm.orRecipientId = data.order.orRecipientId
                this.dataForm.orOrderClassifyId = data.order.orOrderClassifyId
                this.dataForm.orOrderTotalPrice = data.order.orOrderTotalPrice
                this.dataForm.orOrderState = data.order.orOrderState
                this.dataForm.orBuId = data.order.orBuId
                this.dataForm.orCreateDate = data.order.orCreateDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sulingoxqg/order/${!this.dataForm.orId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orId': this.dataForm.orId || undefined,
                'orOrderSn': this.dataForm.orOrderSn,
                'orMemberId': this.dataForm.orMemberId,
                'orRecipientId': this.dataForm.orRecipientId,
                'orOrderClassifyId': this.dataForm.orOrderClassifyId,
                'orOrderTotalPrice': this.dataForm.orOrderTotalPrice,
                'orOrderState': this.dataForm.orOrderState,
                'orBuId': this.dataForm.orBuId,
                'orCreateDate': this.dataForm.orCreateDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
